/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package formatters;

import java.util.Calendar;
import presentation.CurrencyElement;
import presentation.DateElement;
import presentation.LinkElement;
import presentation.PresentationElement;
import presentation.TextElement;
import web.ParamStorage;

/**
 *
 * @author Alejandro
 */
public class InputFormatter implements Formatter {
    private String output;
    private ParamStorage storage;

    public InputFormatter(ParamStorage storage) {
        this.storage = storage;
        output = "";
    }

    @Override
    public String format(PresentationElement e) {
        e.acceptInput(this);
        return output;
    }

    public void formatText(PresentationElement e) {
        TextElement text = (TextElement) e;
        if (storage.hasParam("edit" + text.getName())) {
            output = storage.getParam("edit" + text.getName());
        }
    }

    public void formatLink(PresentationElement e) {
        LinkElement link = (LinkElement) e;
        if (storage.hasParam("edit" + link.getName())) {
            output = storage.getParam("edit" + link.getName());
        }
    }

    public void formatCurrency(PresentationElement e) {
        CurrencyElement currency = (CurrencyElement) e;
        if (storage.hasParam("edit" + currency.getName())) {
            output = storage.getParam("edit" + currency.getName());
        }
    }

    public void formatDate(PresentationElement e) {
        DateElement date = (DateElement) e;
        Calendar c = Calendar.getInstance();
        if (storage.hasParam(date.getName() + "day")) {
            c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(storage.getParam(date.getName() + "day")));
        }
        if (storage.hasParam(date.getName() + "month")) {
            c.set(Calendar.MONTH, Integer.parseInt(storage.getParam(date.getName() + "month"))-1);
        }
        if (storage.hasParam(date.getName() + "year")) {
            c.set(Calendar.YEAR, Integer.parseInt(storage.getParam(date.getName() + "year")));
        }
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        output = c.getTime().toString();
    }
}
